package cn.hamster3.hamsterworld.command;

import cn.hamster3.hamsterworld.core.DataManager;
import cn.hamster3.hamsterworld.core.Message;
import cn.hamster3.hamsterworld.data.WorldData;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.ArrayList;
import java.util.List;

public class TeleportCommand extends CommandBase {
    public TeleportCommand(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public String getPermission() {
        return "hw.tp";
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player player = (Player) sender;
        if (args.length < 2) {
            sender.sendMessage(Message.notInputWorldName.toString());
            return true;
        }
        World world = Bukkit.getWorld(args[1]);

        if (world == null) {
            player.sendMessage(Message.worldNotExist.toString());
            return true;
        }
        WorldData worldData = dataManager.getWorldData(args[1]);
        if (!player.hasPermission("hw.admin")) {
            if (worldData == null) {
                // 非玩家世界
                player.sendMessage(Message.cantTeleportWorld.toString());
                return true;
            }
            if (!worldData.isAllowed(player)) {
                // 不被允许

                OfflinePlayer owner = Bukkit.getOfflinePlayer(worldData.getOwner());
                player.sendMessage(Message.cantEnterWorld.toString().replace("%player%", owner.getName()));
                if (!owner.isOnline()) {
                    return true;
                }
                owner.getPlayer().sendMessage(Message.cantEnterOwnerNotify.toString().replace("%player%", player.getName()));
                return true;
            }
        }

        player.teleport(world.getSpawnLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
        player.sendMessage(Message.tpDone.toString().replace("%world%", world.getName()));
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        ArrayList<String> tab = new ArrayList<>();
        if (args.length == 2) {
            for (WorldData worldData : dataManager.getWorldData()) {
                if (!worldData.getName().toLowerCase().startsWith(args[1].toLowerCase())) {
                    continue;
                }
                if (!worldData.isAllowed((Player) sender)) {
                    continue;
                }
                tab.add(worldData.getName());
            }
            for (String s : dataManager.getWhiteListWorlds()) {
                if (!s.toLowerCase().startsWith(args[1].toLowerCase())) {
                    continue;
                }
                tab.add(s);
            }
        }
        return tab;
    }
}
